const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = 3000;

app.use(require('cors')());
app.use(express.json());

// 获取所有学生
app.get('/api/students', (req, res) => {
  const filePath = path.join(__dirname, 'data', 'students.json');
  fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) return res.status(500).json({ error: '读取失败' });
    res.json(JSON.parse(data));
  });
});

// 保存所有学生（覆盖写入）
app.post('/api/students', (req, res) => {
  const students = req.body;
  const filePath = path.join(__dirname, 'data', 'students.json');
  fs.writeFile(filePath, JSON.stringify(students, null, 2), err => {
    if (err) return res.status(500).json({ error: '写入失败' });
    res.json({ success: true });
  });
});

app.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}`);
}); 